﻿using CommunityToolkit.Mvvm.ComponentModel;
using Materal.Extensions;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows.Controls;

namespace WPFTest
{
    public partial class MainWindowViewModel : ObservableObject
    {
        [ObservableProperty]
        private MenuViewModel? _selectedMenu;
        /// <summary>
        /// 菜单项组
        /// </summary>
        public ObservableCollection<MenuViewModel> MenuItems { get; } = [];
        public event Action<MenuViewModel>? SelectedMenuChanged;
        public MainWindowViewModel()
        {
            IEnumerable<Type> pageTypes = GetType().Assembly.GetTypes().Where(m => m.IsAssignableTo<Page>());
            foreach (Type pageType in pageTypes)
            {
                MenuAttribute? menuAttribute = pageType.GetCustomAttribute<MenuAttribute>();
                if (menuAttribute is null) return;
                MenuViewModel item = new(pageType, menuAttribute);
                MenuItems.Add(item);
            }
        }
        partial void OnSelectedMenuChanged(MenuViewModel? value)
        {
            if (value is null) return;
            SelectedMenuChanged?.Invoke(value);
        }
    }
}
